/*
 * File: vfsdriver.c
 *
 * Description: This is a driver program for testing your VFS system using an interaction script as input
 * You need to make additional calls to your respective functions as noted in the comments below
 * Make sure the output you display is exactly as per the given specifications for you. Do NOT print
 * any extra output (like debug messages) in the final version of your driver program. You can use this driver program
 * in a in incremental manner as you keep implementing one operator after another. For operators not yet implemented,
 * you can leave the output as given ("TO_BE_DONE"). So you can use this program as your "main" program for testing purposes.
 *
 * DO NOT write the full code for operators in the driver program! You must only CALL your functions from here.
 *
 * Usage: vfsdriver <scriptfilename>
 */

/*#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"vfsfunc.h"
#include"diroperations.h"
*/
#include"main.h"
//#include"HashTable.h"// **********************************************
#define BUFSIZE 200
#define CMDSIZE 30
#define PARSIZE 100
//extern struct LinkedList *hashtable[26][26];
void createvfs ( char *P1, int P2 );
void mountvfs ( char *P1 );
void unmountvfs ( char *P1 );
void makedir ( char *P1, char *P2 );
void deletedir ( char *P1 );
void movedir ( char *P1, char *P2 );
void listdir ( char *P1, int P2, char *P3 );
void addfile ( char *P1, char *P2, char *P3 );
void listfile ( char *P1, char *P2 );
void updatefile ( char *P1, char *P2 );
void removefile ( char *P1 );
void movefile ( char *P1, char *P2 );
void copyfile ( char *P1, char *P2 );
void exportfile ( char *P1, char *P2 );
void searchfile ( char *P1, char *P2 );

void processcommand( char *command, char *P1, char *P2, char *P3 );

int main( int argc, char *argv[] )
{
        FILE *scriptfp;
        char linebuffer[BUFSIZE];
        char command[CMDSIZE], par1[PARSIZE], par2[PARSIZE], par3[PARSIZE];
        char *token;
        
        //hash *ht_start_main;
        ht_start=(hash *)malloc(sizeof(hash));

        if( argc != 2 ){
                fprintf(stderr,"Usage: vfsdriver <scriptfile>\n");
                return(1);
        }

        if( (scriptfp=fopen(argv[1],"r")) == NULL ){
                fprintf(stderr,"Unable to open script file: %s\n", argv[1]);
                return(2);
        }

        while( fgets(linebuffer, sizeof(linebuffer), scriptfp) != NULL ){
                /* This output is for debugging... do not uncomment in final version */
                /*
                printf("==================================================\n");
                printf("Processing: %s", linebuffer);
                printf("==================================================\n");
                */

                /* Remove the extra newline character in the end of line */
                linebuffer[ strlen(linebuffer)-1 ] = '\0';

                /* Get the command and the parameters using tokenizer */
                strcpy( command, (token = strtok(linebuffer, " ")) == NULL ? "" : token );

                strcpy( par1, (token = strtok(NULL, " ")) == NULL ? "" : token );
                strcpy( par2, (token = strtok(NULL, " ")) == NULL ? "" : token );
                strcpy( par3, (token = strtok(NULL, " ")) == NULL ? "" : token );
                /* printf("Command:%s:p1:%s:p2:%s:p3:%s\n",command, par1, par2, par3); */

                processcommand( command, par1, par2, par3 );
        }
}

void processcommand( char *command, char *P1, char *P2, char *P3 )
{
        if( strcmp(command, "createvfs") == 0 ){
                int size = atoi(P2);
                createvfs (P1,size);
        }
        else if( strcmp(command, "mountvfs") == 0 )
                mountvfs (P1);
        else if( strcmp(command, "unmountvfs") == 0 )
                unmountvfs (P1);
        else if( strcmp(command, "makedir") == 0 )
                makedir (P1,P2);
        else if( strcmp(command, "deletedir") == 0 )
                deletedir (P1);
        else if( strcmp(command, "movedir") == 0 )
                movedir (P1,P2);
        else if( strcmp(command, "listdir") == 0 ){
                int flag = atoi(P2);
                listdir (P1,flag,P3);
        }
        else if( strcmp(command, "addfile") == 0 )
                addfile (P1,P2,P3);
        else if( strcmp(command, "listfile") == 0 )
                listfile (P1,P2);
        else if( strcmp(command, "updatefile") == 0 )
                updatefile (P1,P2);
        else if( strcmp(command, "removefile") == 0 )
                removefile (P1);
        else if( strcmp(command, "movefile") == 0 )
                movefile (P1,P2);
        else if( strcmp(command, "copyfile") == 0 )
                copyfile (P1,P2);
        else if( strcmp(command, "exportfile") == 0 )
                exportfile (P1,P2);
        else if( strcmp(command, "searchfile") == 0 )
                searchfile (P1,P2);
        else
                printf("Ignoring invalid command %s\n", command);
}

void createvfs ( char *P1, int P2 )
{
        /* Call the appropriate function with given arguments and display appropriate output on the screen */
        //printf("createvfs_TO_BE_DONE\n");
        if(strcmp(P1,"\0")!=0 && P2)
               createprjct(P1,P2);
        else
               printf("ERR_VFS_CREATE: %s", ERR_VFS_CREATE_00);
}

void mountvfs ( char *P1 )
{
        /* Call the appropriate function with given arguments and display appropriate output on the screen */
        //printf("mountvfs_TO_BE_DONE\n");
        if(strcmp(P1,"\0")!=0)
               mountprjct(P1);
        else
               printf("ERR_VFS_MOUNT: %s", ERR_VFS_MOUNT_00);
        //printf("%d",header.used_file_descriptors);

}

void unmountvfs ( char *P1 )
{
        /* Call the appropriate function with given arguments and display appropriate output on the screen */
        //printf("unmountvfs_TO_BE_DONE\n");
        if(strcmp(P1,"\0")!=0)
               unmountprjct(P1);
        else
               printf("ERR_VFS_UNMOUNT: %s", ERR_VFS_UNMOUNT_00);
        //printf("%d",header.used_file_descriptors);
}

void makedir ( char *P1, char *P2 )
{
        /* Call the appropriate function with given arguments and display appropriate output on the screen */
        //printf("makedir_TO_BE_DONE\n");
        if(strcmp(P1,"\0")!=0 && strcmp(P2,"\0")!=0)
        {
               insertdir(P1,P2);
               print_nary(nroot);
         }
         else
          printf("ERR_VFS_MAKEDIR: %s", ERR_VFS_CREATE_00);
}

void deletedir ( char *P1 )
{
        /* Call the appropriate function with given arguments and display appropriate output on the screen */
//      printf("deletedir_TO_BE_DONE\n");
          if(strcmp(P1,"\0")!=0)
               deletedir_31(P1);
          else
                    printf("ERR_VFS_DELETEDIR %s", ERR_VFS_CREATE_00);

}

void movedir ( char *P1, char *P2 )
{
        /* Call the appropriate function with given arguments and display appropriate output on the screen */
        //printf("movedir_TO_BE_DONE\n");
        //movedir_31(P1,P2);
          if(strcmp(P1,"\0")!=0 && strcmp(P2,"\0")!=0)
               movedir_31(P1, P2);
          else
              printf("ERR_VFS_MOVEDIR %s", ERR_VFS_CREATE_00);

}

void listdir ( char *P1, int P2, char *P3 )
{
        /* Call the appropriate function with given arguments and display appropriate output on the screen */
        //printf("listdir_TO_BE_DONE\n");
        //print_nary(nroot);
        if(strcmp(P1,"\0")!=0 && P2 && strcmp(P3,"\0")!=0)
               listdir_31(P1,P2, P3);
        else
              printf("ERR_VFS_LISTDIR %s", ERR_VFS_CREATE_00);
       
}

void addfile ( char *P1, char *P2, char *P3 )
{
        /* Call the appropriate function with given arguments and display appropriate output on the screen */
        //printf("addfile_TO_BE_DONE\n");
//      search_In_HashTable("amar");
        //char dest_dir_path[300]="/vijay",file_name[300]="vijay",data_file_path[300]="/home/vijayhuddar/Desktop/vijay.txt";
        //addfile_vfs31(dest_dir_path,file_name,data_file_path);
       if(strcmp(P1,"\0")!=0 && strcmp(P2,"\0")!=0 && strcmp(P3,"\0")!=0)
               addfile(P1,P2,P3);
        else
              printf("ERR_VFS_ADDFILE %s", ERR_VFS_CREATE_00);
       
        //search_In_HashTable(P2);
        //strcpy(dest_dir_path,"/vijay/vijay");
        //\\search_nary(dest_dir_path);
        //print_nary(nroot);
}

void listfile ( char *P1, char *P2 )
{
        /* Call the appropriate function with given arguments and display appropriate output on the screen */
        //printf("listfile_TO_BE_DONE\n");
        //print_nary(nroot);
         if(strcmp(P1,"\0")!=0 && strcmp(P2,"\0")!=0)
                listfile(P1,P2);
          else
                printf("ERR_VFS_LISTFILE %s", ERR_VFS_CREATE_00);

        
}

void updatefile ( char *P1, char *P2 )
{
        /* Call the appropriate function with given arguments and display appropriate output on the screen */
        //printf("updatefile_TO_BE_DONE\n");
          if(strcmp(P1,"\0")!=0 && strcmp(P2,"\0")!=0)
                updatefile(P1,P2);
          else
                printf("ERR_VFS_UPDATEFILE %s",ERR_VFS_CREATE_00);

}

void removefile ( char *P1 )
{
		int status;
        /* Call the appropriate function with given arguments and display appropriate output on the screen */
        //printf("removefile_TO_BE_DONE\n");
         if(strcmp(P1,"\0")!=0)
         	status=removefile(P1);
          else
            printf("ERR_VFS_REMOVEFILE %s", ERR_VFS_CREATE_00);

        
}

void movefile ( char *P1, char *P2 )
{
		int result;
        /* Call the appropriate function with given arguments and display appropriate output on the screen */
        //printf("movefile_TO_BE_DONE\n");
        if(strcmp(P1,"\0")!=0 && strcmp(P2,"\0")!=0)
               result=movefile(P1,P2 );
          else
                printf("ERR_VFS_MOVEFILE %s", ERR_VFS_CREATE_00);
}

void copyfile ( char *P1, char *P2 )
{
	int status;
        /* Call the appropriate function with given arguments and display appropriate output on the screen */
        //printf("copyfile_TO_BE_DONE\n");
        //inorder(bst_root);
        if(strcmp(P1,"\0")!=0 && strcmp(P2,"\0")!=0)
             status=copyfile(P1,P2);\
          else
                printf("ERR_VFS_COPYFILE %s", ERR_VFS_CREATE_00);
}
       
    

void exportfile ( char *P1, char *P2 )
{
        /* Call the appropriate function with given arguments and display appropriate output on the screen */
        //printf("exportfile_TO_BE_DONE\n");
        if(strcmp(P1,"\0")!=0 && strcmp(P2,"\0")!=0)
                 exportfile(P1,P2);
          else
                printf("ERR_VFS_EXPORTFILE %s", ERR_VFS_CREATE_00);
        
}

void searchfile ( char *P1, char *P2 )
{
	int return_val;
        /* Call the appropriate function with given arguments and display appropriate output on the screen */
        //printf("searchfile_TO_BE_DONE\n");
        if(strcmp(P1,"\0")!=0 && strcmp(P2,"\0")!=0)
               return_val=searchfile(P1,P2);
          else
                printf("ERR_VFS_SEARCHFILE %s", ERR_VFS_CREATE_00);


}



